package com.datastructure.test.shortestsubarray;

public class ShortestSubarray {

    public static void main(String[] args) {

    }

    public int shortestSubarray (int[] nums, int k) {
        // write code here
        int start = 0, end = 0;
        int n = nums.length;
        int sum = 0, ret = Integer.MAX_VALUE;
        while (end < n) {
            sum += nums[end];
            while (sum>=k){
                ret = Math.min(ret, end-start+1);
                sum -= nums[start++];
            }
            end++;
        }
        return ret==Integer.MAX_VALUE?-1:ret;
    }
}
